package com.lemontree.spring.aoptalk;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibAop implements MethodInterceptor {

    private Enhancer enhancer = new Enhancer();

    public Object getProxy(Object obj){
        enhancer.setSuperclass(obj.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("cglib....");
        Object result = proxy.invokeSuper(obj, args);
        System.out.println("cglib....");
        return result;
    }
}
